<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>
<body>
  <div id="app">
    <h2>自定义指令</h2>
    <input type="text" v-focus>
    <h3 v-detail:qwer.denglei.shixj="colorVal"></h3>
    <div v-demo="{color: colorVal,text: 'hello'}"></div>
    <input type="text" v-model="colorVal">
  </div>
  <script src="../js/vue.js"></script>
  <script>
    let vm = new Vue({
      el: '#app',
      data: {
        colorVal: 'red'
      },
      // 局部注册
      directives: {
        focus: {
          // 功能：使绑定的元素获取光标
          inserted: function(el) {
            el.focus()
          }
        },
        detail: {
          // 展示自定义指令的内部情况
          bind: function(el, binding, vnode){
            let str = JSON.stringify;
            el.innerHTML = 
              `
              el: el<br>
              name: ${str(binding.name)}<br>
              value: ${str(binding.value)}<br>
              expression: ${str(binding.expression)}<br>
              argument: ${str(binding.arg)}<br>
              modifiers: ${str(binding.modifiers)}<br>
              vnode: ${Object.keys(vnode).join(', ')}<br>
              +++${binding.value}
              `
          }
        },
        demo: {
          bind: function(el, binding, vnode) {
            console.log(111,binding)
            console.log(111,vnode)
          },
          update: function(el, binding, vnode) {
            console.log(222, binding)
            console.log(222, vnode)
          }
        }
      }
    })
  </script>
</body>
</html>